import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:manager/constant/constant.dart';

class DioManager {
  static DioManager _instance;

  static DioManager getInstance() {
    if (_instance == null) {
      _instance = DioManager();
    }
    return _instance;
  }

  Map<String, dynamic> headers = {
    "Accept": "application/json",
    "Content-Type": "application/json; charset=UTF-8",
  };

  Dio dio = new Dio();

  DioManager() {
    dio.options.baseUrl = Constant.baseURL;
    dio.options.connectTimeout = 5000;
    dio.options.receiveTimeout = 3000;
    dio.options.headers = headers;
    //返回value为int的时候报错的bug
    //dio.options.responseType=ResponseType.plain;
    //是否开启请求日志
    dio.interceptors.add(LogInterceptor(responseBody: true));
    //缓存
    //dio.interceptors.add(CookieManager(CookieJar()));
  }

  //get请求
  get(String url, FormData params, Function successCallBack,
      Function errorCallBack) async {
    _requestHttp(url, successCallBack, 'get', params, errorCallBack);
  }

  //post请求
  post(String url, params, Function successCallBack,
      Function errorCallBack) async {
    _requestHttp(url, successCallBack, 'post', params, errorCallBack);
  }

  postNoParams(
      String url, Function successCallBack, Function errorCallBack) async {
    _requestHttp(url, successCallBack, 'post', null, errorCallBack);
  }

  _requestHttp(String url, Function successCallBack,
      [String method, FormData params, Function errorCallBack]) async {
    Response response;
    try {
      if (method == 'get') {
        if (params != null && params.isNotEmpty) {
          response = await dio.get(url, queryParameters: params);
        } else {
          response = await dio.get(url);
        }
      } else if (method == 'post') {
        if (params != null && params.isNotEmpty) {
          response = await dio.post(url, data: params);
        } else {
          response = await dio.post(url);
        }
      }
    } on DioError catch (error) {
      //请求错误处理
      /*if (error.response != null) {
        response = error.response;
      } else {
        response = new Response(statusCode: 201);
      }*/
      if (Constant.ISDEBUG) {
        print('请求异常:' + error.toString());
      }
      _error(errorCallBack, error.message);
      return '';
    }

    if (Constant.ISDEBUG) {
      print("请求URL：" + url);
      print("请求头：" + dio.options.headers.toString());
      if (params != null) {
        print("请求参数：" + params.toString());
      }
      if (response != null) {
        print("返回参数：" + response.toString());
      }
    }
    String dataStr = json.encode(response.data);
    Map<String, dynamic> dataMap = json.decode(dataStr);
    if (dataMap == null || dataMap['code'] != 200) {
      _error(errorCallBack, dataMap['message'].toString());
    } else if (successCallBack != null) {
      successCallBack(dataMap['data']);
    }
  }

  _error(Function errorCallBack, String error) {
    if (errorCallBack != null) {
      errorCallBack(error);
    }
  }
}
